package com.example.hidesms;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ProgressBar;

import com.example.hidesms.adapter.CoolTitleAdapter;
import com.example.hidesms.dto.CoolTitleItem;
import com.example.hidesms.dto.SMSItem;
import com.example.hidesms.receiver.SmsFilter;
import com.example.hidesms.utils.Utils;

public class ListCoolSMS extends Fragment {

	private GridView					gridView;
	private ArrayList<CoolTitleItem>	lstSMS;
	private CoolTitleAdapter			mAdapter;
	private ProgressBar					mProgressBar;
	private SmsFilter					smsFilter;
	private ListCoolLitener				coolLitener;

	// private ArrayList<CoolTitleItem> listTitle ;

	public void setHomeListener(ListCoolLitener coolLitener) {
		this.coolLitener = coolLitener;
	}

	public interface ListCoolLitener extends Parcelable {
		public void startCate(String address);

		public void addListBlock();

		public void addListBlockIncoming();

		public void addListBlockOutgoing();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View view = inflater.inflate(R.layout.cool_sms_screen, container, false);
		getElementFromUI(view);
		return view;
	}

	private void getElementFromUI(View view) {
		gridView = (GridView) view.findViewById(R.id.list);
		mProgressBar = (ProgressBar) view.findViewById(R.id.progressBar);
		if (smsFilter == null) {
			smsFilter = new SmsFilter();
			getActivity().registerReceiver(smsFilter, new IntentFilter("android.provider.Telephony.SMS_RECEIVED"));
		}
		getData();
//		gridView.setOnItemClickListener(new OnItemClickListener() {
//
//			@SuppressWarnings("unchecked")
//			@Override
//			public void onItemClick(AdapterView<?> adapter, View arg1, int pos, long arg3) {
//				ArrayAdapter<SMSItem> item = (ArrayAdapter<SMSItem>) adapter.getAdapter();
//				String str = item.getItem(pos).getAddressNameS();
//				coolLitener.startCate(str);
//			}
//		});

	}

	private void getData() {
		mProgressBar.setVisibility(View.VISIBLE);
		try {
			lstSMS = Utils.getTitleFromXML(getActivity());

			Thread th = new Thread(new Runnable() {

				@Override
				public void run() {
					Log.e("getdata", "<0 " + lstSMS.size());

					if (lstSMS.size() > 0) {
						getActivity().runOnUiThread(new Runnable() {

							@Override
							public void run() {
								if (mAdapter == null) {
									mAdapter = new CoolTitleAdapter(getActivity(), -1, lstSMS);
									// mAdapter.setHomeListener(homeListener);
									gridView.setAdapter(mAdapter);
								} else {
									mAdapter.notifyDataSetChanged();
								}
								mProgressBar.setVisibility(View.GONE);
							}
						});
					}

				}
			});
			th.start();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, 123, 1, "fuck");
		menu.add(1, 321, 3, "fuck");
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		SubMenu sub = menu.addSubMenu("Group");// .setIcon(R.drawable.tab_selected);
		sub.add(0, R.style.AppTheme, 0, "Date");
		sub.add(0, R.style.AppTheme, 0, "Phone");
		// sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		// MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		SubMenu sub1 = menu.addSubMenu("Filter");// .setIcon(R.drawable.group_selected);
		sub1.add(0, R.style.AppTheme, 0, "Default");
		sub1.add(0, R.style.AppTheme, 0, "Light");
		// sub1.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS |
		// MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (smsFilter != null) {
			getActivity().unregisterReceiver(smsFilter);
			smsFilter = null;
		}
	}
	public static ListCoolSMS instance(){
		ListCoolSMS fragment = new ListCoolSMS();
		Bundle bunle = new Bundle();
		fragment.setArguments(bunle);
		return fragment;
		
	}

	private void getSMS() {
		ArrayList<SMSItem> lstSMS = Utils.getAllSms(getActivity());
		String msgData = "";
		for (int i = 0; i < lstSMS.size(); i++) {
			msgData += "Content: " + lstSMS.get(i).getMsg();
		}
		Log.e("getSMS", " " + msgData);
	}

}
